package com.scau.controller;

import com.scau.entity.Menu;
import com.scau.entity.MenuVO;
import com.scau.entity.User;
import com.scau.entity.UserVO;
import com.scau.fegin.MenuFegin;
import com.scau.fegin.UserFegin;
import com.scau.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Value("${web.menu-pic-path}")
    private String imagesPrefix;

    @Autowired
    private MenuFegin menuFegin;

    @GetMapping(value = "/redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        log.info("请求跳转  " + location);
        return location;
    }

    @PostMapping(value = "/save",produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public String save(Menu menu, @RequestParam(value = "image" ,required = false) MultipartFile image){

        String filePath = "/images/"+menu.getId()+"/";

        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String pid = UUID.randomUUID().toString().replace("-","");
        fileName =  pid+suffix;

        System.out.println(filePath+fileName);

        System.out.println(this.imagesPrefix+menu.getId()+"\\"+fileName);

        FileUtil.createDir(this.imagesPrefix+menu.getId());
        FileUtil.saveFile(image,this.imagesPrefix+menu.getId()+"\\"+fileName);

        menu.setLogoUrl(filePath+fileName);
        menuFegin.save(menu);
        return "success";
    }

    @GetMapping("/findAll")
    @ResponseBody
    public MenuVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        System.out.println(page + " " +limit);
        log.info("findAll  ");
        return menuFegin.findAll((page-1)*limit,limit);
    }

    @GetMapping("/findById/{id}")
    @ResponseBody
    public Menu findById(@PathVariable("id") int id){
        System.out.println(id);
        Menu menu = menuFegin.findById(id);
        log.info("findById  ");
        return menu;
    }
//
//    @PostMapping(value = "/update")
//    @ResponseBody
//    public String update(User user,@RequestParam(value = "image" ,required = false) MultipartFile image){
//        System.out.println(user);
//
//        String filePath = "/uploads/"+user.getId()+"/";
//
//        String fileName = image.getOriginalFilename();
//        String suffix = fileName.substring(fileName.lastIndexOf("."));
//        String pid = UUID.randomUUID().toString().replace("-","");
//        fileName =  pid+suffix;
//
//        System.out.println(filePath+fileName);
//
//        System.out.println(this.fileprefix+user.getId()+"\\"+fileName);
//
//        FileUtil.deleteDir(this.fileprefix+user.getId());
//        FileUtil.createDir(this.fileprefix+user.getId());
//        FileUtil.saveFile(image,this.fileprefix+user.getId()+"\\"+fileName);
//
//        user.setLogoUrl(filePath+fileName);
//        userFegin.update(user);
//        return "success";
//    }


}
